package cfsmon.logic.nfm;

import java.io.InputStream;

import org.apache.log4j.Logger;

import cfsmon.core.Frontend;
import cfsmon.logic.dfm.IDfm;
import cfsmon.logic.nfm.declParser.NfmDeclParser;

public class NfmFrontend implements Frontend {

	private static Logger logger = Logger.getLogger(NfmFrontend.class);
	
	@Override
	public IDfm compile(InputStream in) throws Exception{
		// parsing / lexing / validation
		
		logger.info("Starting lexing, parsing.");
		INfm nfm = NfmDeclParser.parse(in);
		if (nfm != null){
			logger.info("Parsing successful.");
		} else {
			logger.error("Parsing failed.");
			return null;
		}
		logger.info("Starting input validation.");
		
		if (NfmInputValidator.isValid(nfm)){
			logger.info("Validation successful.");
		} else {
			logger.error("Validation failed.");
			return null;
		}
		// optimization
		logger.info("Starting input optimization.");
		INfm optNfm = NfmOptimizer.optimize(nfm);
		
		if (optNfm != null){
			logger.info("Optimization successful.");
		} else {
			logger.error("Optimization failed.");
			return null;
		}
		// translation to dfm
		logger.info("Starting translation to DFM.");
		IDfm dfm = NfmToDfmTranslator.translateToDfm(optNfm);
		if (dfm != null){
			logger.info("Translation successful.");
		} else {
			logger.error("Translation failed.");
		}
		// output
		return dfm;
	}

}
